package com.wanli.agent.homepage.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.RecyclerView;

import com.google.gson.Gson;
import com.wanli.agent.R;
import com.wanli.agent.bean.BusinessProviderInfoBean;
import com.wanli.agent.bean.UserInfoBean;
import com.wanli.agent.homepage.AgentDetailsActivity;
import com.wanli.agent.homepage.BusinessDataDetailActivity;
import com.wanli.agent.utils.SPManager;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Author: HWC
 * Description: 服务商列表
 */
public class ProviderDataListAdapter extends RecyclerView.Adapter<ProviderDataListAdapter.ViewHolder> {

    private List<BusinessProviderInfoBean.DataBean> listsBeans;
    private Context context;
    private UserInfoBean userInfoBean;
    private String startTime;
    private String endTime;
    private String month;
    private String yesterday;
    private int roleFlag;

    public ProviderDataListAdapter(List<BusinessProviderInfoBean.DataBean> listsBeans) {
        this.listsBeans = listsBeans;
        userInfoBean = new Gson().fromJson(SPManager.getInstance().getUserInfoJson(), UserInfoBean.class);
    }

    public void setData(String yesterday, String month, String startTime, String endTime, int roleFlag){
        this.yesterday = yesterday;
        this.month = month;
        this.startTime = startTime;
        this.endTime = endTime;
        this.roleFlag = roleFlag;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int viewType) {
        context = viewGroup.getContext();
        View inflate = LayoutInflater.from(context).inflate(R.layout.item_data_provider, viewGroup, false);
        ViewHolder viewHolder = new ViewHolder(inflate);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        BusinessProviderInfoBean.DataBean dataBean = listsBeans.get(position);
        holder.tvName.setText(dataBean.getName());
        holder.tvDealAmount.setText(dataBean.getTrade_money());
        holder.tvNewMerchantCount.setText(dataBean.getAdd_merchant());
        int channel_type = Integer.parseInt(dataBean.getChannel_type());
        if (channel_type == 1) {
            holder.tvChannelType.setText("标");
            holder.tvChannelType.setBackground(ContextCompat.getDrawable(context, R.drawable.bg_commercial_1));
        } else if (channel_type == 2) {
            holder.tvChannelType.setText("智");
            holder.tvChannelType.setBackground(ContextCompat.getDrawable(context, R.drawable.bg_commercial_4));
        }

        holder.itemView.setOnClickListener(v -> {
            if (userInfoBean != null) {
                Intent intent = new Intent(context, BusinessDataDetailActivity.class);
                intent.putExtra("branchName", dataBean.getName());
                intent.putExtra("productVersion", dataBean.getChannel_type());
                intent.putExtra("dealAmount", dataBean.getTrade_money());
                intent.putExtra("merchantCountOrDealCount", dataBean.getAdd_merchant());
                intent.putExtra("roleFlag", roleFlag);
                intent.putExtra("agentId", dataBean.getId() + "");
                intent.putExtra("yesterday", yesterday);
                intent.putExtra("month", month);
                intent.putExtra("startTime", startTime);
                intent.putExtra("endTime", endTime);
                context.startActivity(intent);
            }
        });

    }

    @Override
    public int getItemCount() {
        return listsBeans.size() == 0 ? 0 : listsBeans.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.tvChannelType)
        TextView tvChannelType;
        @BindView(R.id.tvName)
        TextView tvName;
        @BindView(R.id.tvDealAmount)
        TextView tvDealAmount;
        @BindView(R.id.tvNewMerchantCount)
        TextView tvNewMerchantCount;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}
